//
//  BCProjectDetailRichCollectionCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/26.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCProjectDetailRichCollectionCell.h"


@interface BCProjectDetailRichCollectionNumberView : UIView

/**
 *backgroundLayer
 */
@property (nonatomic, strong) CAShapeLayer *backgroundLayer;

/**
 *numberIV
 */
@property (nonatomic, strong) UIImageView *numberIV;

/**
 *numberLabel
 */
@property (nonatomic, strong) UILabel *numberLabel;

- (void)setNumberImage:(UIImage *)image number:(NSString *)number;

@end

@implementation BCProjectDetailRichCollectionNumberView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:[UIColor clearColor]];
    //
    self.backgroundLayer.fillColor = backgroundColor.CGColor;
    self.numberLabel.textColor = [backgroundColor qmui_colorIsDark]?[UIColor whiteColor]:[UIColor qmui_colorWithHexString:@"#666666"];
}

- (void)setupSubviews {
    self.backgroundLayer = [CAShapeLayer layer];
    self.backgroundLayer.strokeColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:self.backgroundLayer];
    //
    //
    self.numberIV = [UIImageView new];
    [self addSubview:self.numberIV];
    //
    self.numberLabel = [UILabel new];
    self.numberLabel.textColor = [UIColor whiteColor];
    self.numberLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    [self addSubview:self.numberLabel];
    //
    [self.numberIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(30.0);
        make.left.mas_equalTo(self.mas_centerX).offset(-15.0);
    }];
    //
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.left.mas_equalTo(self.numberIV.mas_right).offset(2.0);
    }];
}

- (void)setNumberImage:(UIImage *)image number:(NSString *)number {
    self.numberIV.image = image;
    self.numberLabel.text = number;
    [self.numberLabel sizeToFit];
    //
    CGFloat width = 30.0+2.0+CGRectGetWidth(self.numberLabel.frame);
    [self.numberIV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_centerX).offset(-width/2-3.0);
    }];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    //
    self.backgroundLayer.frame = self.bounds;
    //
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    CGFloat arcRadius = height/2;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, height/2)];
    [path addArcWithCenter:CGPointMake(arcRadius, arcRadius) radius:arcRadius startAngle:M_PI endAngle:M_PI*1.5 clockwise:true];
    [path addLineToPoint:CGPointMake(width-arcRadius, 0)];
    [path addArcWithCenter:CGPointMake(width-arcRadius, arcRadius) radius:arcRadius startAngle:-M_PI/2 endAngle:M_PI/2 clockwise:true];
    [path addLineToPoint:CGPointMake(0, height)];
    [path closePath];
    //
    self.backgroundLayer.path = path.CGPath;
}

@end

@interface BCProjectDetailRichCollectionCell ()

@property (nonatomic, strong) UIView *customContentView;

/**
 *headIV
 */
@property (nonatomic, strong) UIImageView *headIV;

/**
 *BCProjectDetailRichCollectionNumberView
 */
@property (nonatomic, strong) BCProjectDetailRichCollectionNumberView *numberView;


@property (nonatomic, strong) UILabel *captionLabel;
@property (nonatomic, strong) UILabel *coinLabel;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation BCProjectDetailRichCollectionCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews {
    self.customContentView = [UIView new];
    [self.contentView addSubview:self.customContentView];
    [self.customContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(5.0);
        make.right.mas_equalTo(self.contentView).offset(-5.0);
        make.top.bottom.mas_equalTo(self.contentView);
    }];
    //
    CGFloat headWidth = 58.0;
    self.headIV = [UIImageView new];
    self.headIV.contentMode = UIViewContentModeScaleAspectFill;
    self.headIV.clipsToBounds = true;
    [self.customContentView addSubview:self.headIV];
    [self.headIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.centerX.mas_equalTo(self.customContentView);
        make.size.mas_equalTo(CGSizeMake(headWidth, headWidth));
    }];
    self.headIV.layer.cornerRadius = headWidth/2;
    //
    self.numberView = [BCProjectDetailRichCollectionNumberView new];
    [self.customContentView addSubview:self.numberView];
    [self.numberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.headIV);
        make.top.mas_equalTo(self.headIV.mas_bottom).offset(-8.0);
        make.width.mas_equalTo(self.headIV).offset(24.0);
        make.height.mas_equalTo(22.0);
    }];
    //
    self.captionLabel = [UILabel new];
    self.captionLabel.textAlignment = NSTextAlignmentCenter;
    self.captionLabel.font = [UIFont systemFontOfSize:11 weight:UIFontWeightRegular];
    self.captionLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.captionLabel.adjustsFontSizeToFitWidth = true;
    [self.customContentView addSubview:self.captionLabel];
    [self.captionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(self.customContentView);
        make.height.mas_equalTo(22.0);
    }];
    self.coinLabel = [UILabel new];
    self.coinLabel.textAlignment = NSTextAlignmentCenter;
    self.coinLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    self.coinLabel.textColor = [UIColor darkMainColor];
    self.coinLabel.adjustsFontSizeToFitWidth = true;
    [self.customContentView addSubview:self.coinLabel];
    [self.coinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.captionLabel.mas_top);
        make.left.right.mas_equalTo(self.customContentView);
        make.height.mas_equalTo(self.coinLabel.font.lineHeight);
    }];
    //
    self.nameLabel = [UILabel new];
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    self.nameLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    self.nameLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    [self.customContentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.numberView.mas_bottom);
        make.bottom.mas_equalTo(self.coinLabel.mas_top);
        make.right.left.mas_equalTo(self.customContentView);
    }];
}

- (void)setCellData:(BCProjectDetailDataTagsData *)cellData {
    _cellData = cellData;
    //
    [self.headIV sd_setImageWithURL:[NSURL URLWithString:cellData.head_img]];
    //
    self.nameLabel.text = cellData.username;
    self.coinLabel.text = cellData.gain_amount;
    self.captionLabel.text = cellData.text;
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    //
    switch (indexPath.row) {
        case 0:
        {
            [self.numberView setNumberImage:LOAD_IMAGE(@"project_rich_number_gold") number:@"NO.1"];
            //
            self.numberView.backgroundColor = [UIColor qmui_colorWithHexString:@"#F0813B"];
        }
            break;
        case 1:
        {
            [self.numberView setNumberImage:LOAD_IMAGE(@"project_rich_number_silver") number:@"NO.2"];
            //
            self.numberView.backgroundColor = [UIColor qmui_colorWithHexString:@"#2A929B"];
        }
            break;
        case 2:
        {
            [self.numberView setNumberImage:LOAD_IMAGE(@"project_rich_number_copper") number:@"NO.3"];
            //
            self.numberView.backgroundColor = [UIColor qmui_colorWithHexString:@"#DBA503"];
        }
            break;
        default:
            [self.numberView setNumberImage:nil number:nil];
            //
            self.numberView.backgroundColor = [UIColor clearColor];
            break;
    }
}


+ (CGFloat)defaultHeight {
    return 148.0;
}

@end
